package jiubeirobot.com.robotshop.ui.activity.shop;

import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import jiubeirobot.com.robotshop.R;
import jiubeirobot.com.robotshop.base.Activitys;
import jiubeirobot.com.robotshop.base.BaseActivity;
import jiubeirobot.com.robotshop.base.BaseBean;
import jiubeirobot.com.robotshop.bean.StockBean;
import jiubeirobot.com.robotshop.bean.UserSubmitOrderPramsBean;
import jiubeirobot.com.robotshop.data.constans.Constants;
import jiubeirobot.com.robotshop.data.net.APIManager;
import jiubeirobot.com.robotshop.data.net.Flags;
import jiubeirobot.com.robotshop.ui.fragment.shop.SelectInvoiceTypeFragment;
import jiubeirobot.com.robotshop.ui.fragment.shop.ShopManifestFragment;
import jiubeirobot.com.robotshop.utils.Arith;
import jiubeirobot.com.robotshop.utils.L;
import jiubeirobot.com.robotshop.utils.glide.GlideImageLoadImpl;

public class CheckOrderActivity extends BaseActivity {
    @BindView(R.id.iv_search_icon)
    ImageView ivsearch;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.llyt_back)
    LinearLayout llback;
    @BindView(R.id.btn_submit_order)
    Button btnsuborder;
    @BindView(R.id.llyt_invoice)
    LinearLayout llytinvoice;
    @BindView(R.id.dl_drawer)
    DrawerLayout dlDrawer;
    @BindView(R.id.llyt_shop_list)
    LinearLayout llytshoplist;
    @BindView(R.id.tv_invoiceType)
    TextView tvInvoiceType;
    @BindView(R.id.rb_account)
    RadioButton rb_account;
    @BindView(R.id.rb_no_account)
    RadioButton rb_noaccount;

    @BindView(R.id.iv_shop_1)
    ImageView ivShop1;
    @BindView(R.id.iv_shop_2)
    ImageView ivShop2;
    @BindView(R.id.iv_shop_3)
    ImageView ivShop3;
    @BindView(R.id.iv_shop_4)
    ImageView ivShop4;
    @BindView(R.id.iv_more)
    ImageView ivMore;

    @BindView(R.id.tv_total_shop_num)
    TextView tvTotalShopNum;
    @BindView(R.id.tv_total_money)
    TextView tvTotalMoney;
    @BindView(R.id.tv_shop_total_money)
    TextView tvShopTotalMoney;
    @BindView(R.id.tv_be_paid)
    TextView tvBePaid;



    SelectInvoiceTypeFragment selectInvoiceTypeFragment;
    ShopManifestFragment shopManifestFragment;
    private List<StockBean> cardList = new ArrayList<>();
    private UserSubmitOrderPramsBean pramsbean = new UserSubmitOrderPramsBean();//发票数据
    private String shopTotalmoney;
    private int shopTotalNum;//商品总数量


    @Override
    public int pageLayout() {
        return R.layout.activity_check_order;
    }

    @Override
    public void initTitle() {
        ivsearch.setVisibility(View.GONE);
        tvTitle.setText(getString(R.string.check_order));
    }

    @Override
    public void initView() {
        int size = 0;
        Serializable seri = getIntent().getSerializableExtra(Constants.INTENT_STOCK_BEAN);
        if (seri != null) {
            StockBean stockBean = (StockBean) seri;
            shopTotalNum = size;
            size = cardList.size();
            shopTotalmoney = (stockBean.getNum() * Double.parseDouble(stockBean.getGoods_current_price() + "")) + "";
            cardList.add(stockBean);
        }

        if (size > 0) {
            GlideImageLoadImpl.getInstance().load(this, cardList.get(0).getGoods_img(), ivShop1);
            ivShop1.setVisibility(View.VISIBLE);
            if (size > 1) {
                GlideImageLoadImpl.getInstance().load(this, cardList.get(1).getGoods_img(), ivShop2);
                ivShop2.setVisibility(View.VISIBLE);
            }
            if (size > 2) {
                GlideImageLoadImpl.getInstance().load(this, cardList.get(2).getGoods_img(), ivShop3);
                ivShop2.setVisibility(View.VISIBLE);
            }
            if (size > 3) {
                GlideImageLoadImpl.getInstance().load(this, cardList.get(3).getGoods_img(), ivShop4);
                ivShop3.setVisibility(View.VISIBLE);
                ivMore.setVisibility(View.VISIBLE);
            }
        } else {
            ivMore.setVisibility(View.GONE);
        }

        tvTotalShopNum.setText("共" + shopTotalNum + "件商品");
        tvTotalMoney.setText("￥" + Arith.get2Decimal(shopTotalmoney));
        tvShopTotalMoney.setText("￥" + Arith.get2Decimal(shopTotalmoney));
        tvBePaid.setText("￥" + Arith.get2Decimal(shopTotalmoney));

    }

    @Override
    protected boolean isOpenStatus() {
        return super.isOpenStatus();
    }

    public void setInvoiceType(UserSubmitOrderPramsBean invoiceType) {
        if ("1".equals(invoiceType.getType())) {
            tvInvoiceType.setText(getString(R.string.shopping_check_no_invoice));
        } else if ("2".equals(invoiceType.getType())) {
            tvInvoiceType.setText(getString(R.string.shopping_check_general_invoice));
        } else if ("3".equals(invoiceType.getType())) {
            tvInvoiceType.setText(getString(R.string.shopping_check_add_val_invoice));
        }
        this.pramsbean = invoiceType;
        L.i(pramsbean);
    }

    @Override
    public void onSuccess(BaseBean bean, int flag) {
        switch (flag) {
            case Flags.FLAG_USER_ORDER_SUBMIT:
                break;

            default:
                break;
        }
        dismissHUD();
    }

    @OnClick({R.id.llyt_back, R.id.btn_submit_order, R.id.llyt_invoice, R.id.llyt_shop_list})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.llyt_back:
                finish();
                break;
            case R.id.btn_submit_order:


                APIManager.getInstance().orderSubmit(CheckOrderActivity.this, this, pramsbean);
                Activitys.toSelectType(CheckOrderActivity.this, shopTotalmoney);
                break;
            case R.id.llyt_invoice:
                showSelectInvoiceType();
                break;
            case R.id.llyt_shop_list:
                showShopManifest();
                break;
            default:
                break;
        }
    }
    /**
     * 显示商品清单
     */
    public void showShopManifest() {
        if (shopManifestFragment == null) {
            shopManifestFragment = new ShopManifestFragment();
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_content, shopManifestFragment);
        transaction.commit();

        if (dlDrawer.isDrawerOpen(Gravity.RIGHT)) {
            dlDrawer.closeDrawer(Gravity.RIGHT);
        } else {
            dlDrawer.openDrawer(Gravity.RIGHT);
            shopManifestFragment.setList(cardList);
        }
    }

    public void close() {
        dlDrawer.closeDrawer(Gravity.RIGHT);
    }

    /**
     * 点击显示选发票的fragment
     */
    private void showSelectInvoiceType() {
        if (selectInvoiceTypeFragment == null) {
            selectInvoiceTypeFragment = new SelectInvoiceTypeFragment();
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_content, selectInvoiceTypeFragment);
        transaction.commit();
        if (dlDrawer.isDrawerOpen(Gravity.RIGHT)) {
            dlDrawer.closeDrawer(Gravity.RIGHT);
        } else {
            dlDrawer.openDrawer(Gravity.RIGHT);
        }
    }
}
